package com.net.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.net.model.Category;
import com.net.model.Commodity;
import com.net.model.Provider;
import com.net.service.CategoryService;
import com.net.service.CommodityService;
import com.net.service.ProviderService;
import com.opensymphony.xwork2.ActionSupport;

@Component("Commodity-*")
@Scope("prototype")
public class CommodityAction extends ActionSupport {
	private Commodity commodity;
	private Provider provider;
	private List<Provider> providers;
	private Category category;
	private List<Category> categories;
	private CommodityService commodityService;
	private List<Commodity> commodities;
	private String[] selectedList;
	private CategoryService categoryService;
	private ProviderService providerService;
	private String json1;
	
	public String addCommit() throws Exception {
		// System.out.println(commodity.getCommodity_name()+"llllll");
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				Commodity commodity = commodities.get(Integer.parseInt(selectedList[i]));
				if (commodity.getCommodity_name().equals("")||commodity.getCategory().getId()==0) {
					
				} else {
					commodity.setProviders(null);
					category =categoryService.loadCategoryById(commodity.getCategory().getId());
					commodity.setCategory(category);
					this.commodityService.add(commodity);
				}
			}
		}
		this.add();
		this.list();
		return "addCommodityCommit";
	}

	public String add() throws Exception {
		categories = categoryService.getCategories();
		providers = providerService.getProviders();
		return "addCommodity";
	}
	
	public String delete() throws Exception {
		System.out.println(commodity.getId()+"999999999");
		commodity.setCategory(null);
		commodity.setProviders(null);
		this.commodityService.delete(commodity);
		this.list();
		// System.out.println(commodity.getId()+":"+commodity.getcommodity_name());
		return "deleteCommodity";
	}

	public String deleteSelected() throws Exception {
		// System.out.println(selectedList.length+"  :8888888888888888");
		Commodity cust = new Commodity();
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				cust.setId(Integer.parseInt(selectedList[i]));
				this.commodityService.delete(cust);
			}
		}
		this.list();
		return "deleteSelectedCommodity";
	}

	public String list() throws Exception {
		this.commodities = commodityService.getCommodities();
		// System.out.println(commoditys.get(0).getShort_name()+"++++++++++++++");
		return "listCommodity";
	}

	public String load() {
		this.commodity = this.commodityService.loadCommodityById(commodity.getId());
		return "loadCommodity";
	}

	public String update() throws Exception {
		this.add();
		 System.out.println(commodity.getId()+"#################");
		commodity = commodityService.loadCommodityById(commodity.getId());
		 System.out.println(commodity.getCommodity_name()+"#################");
		return "updateCommodity";
	}

	public String updateCommit() throws Exception {
		System.out.println(commodity.getId()+"*****##########");
		category = categoryService.loadCategoryById(commodity.getCategory().getId());
		commodity.setCategory(category);
		commodityService.update(commodity);
		this.list();
		return "updateCommodityCommit";
	}
	
	public String search() throws Exception{
		this.add();
		System.out.println("123");
		return "searchCommodity";
	}

	public String searchCommit() throws Exception {
		//System.out.println(commodity.getId()+"*****000"+commodity.getCommodity_name()+"+++"+commodity.getCategory().getId());
		
		System.out.println(provider.getId()+"*****99999");
		if(provider.getId()!=0){
			provider = providerService.loadProviderById(provider.getId());
			commodities = new ArrayList(provider.getCommodities()); 
		}else{
			this.commodities = commodityService.searchList(commodity);
		}
		this.add();
		return "searchCommodityCommit";
	}
	//ajax应用[{"category_name":"345rwer","discription":"","id":1},{"category_name":"rtrtrt","discription":"","id":2},{"category_name":"wewe","discription":"","id":3},{"category_name":"clo","discription":"","id":4}]
	public String categoryType() throws Exception{
		categories = categoryService.getCategories();
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(Category ca : categories){
			sb.append("{\"id\":").append(ca.getId()+",").append("\"category_name\":\"").append(ca.getCategory_name() + "\"},");
		}
		sb.replace(sb.length()-1, sb.length(), "]");
		//JSONArray jsonObject = new JSONArray();
		//json1=jsonObject.fromObject(categories).toString();
		json1 = sb.toString();
		System.out.println(json1+"*****##########");
		return SUCCESS;
	}
	
	public String providerType() throws Exception{
		providers = providerService.getProviders();
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(Provider ca : providers){
			sb.append("{\"id\":").append(ca.getId()+",").append("\"provider_name\":\"").append(ca.getProvider_name() + "\"},");
		}
		sb.replace(sb.length()-1, sb.length(), "]");
		//JSONArray jsonObject = new JSONArray();
		//json1=jsonObject.fromObject(categories).toString();
		json1 = sb.toString();
		System.out.println(json1);
		return SUCCESS;
	}
	
	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}

	public CommodityService getCommodityService() {
		return commodityService;
	}

	@Resource(name = "commodityService")
	public void setCommodityService(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	public List<Commodity> getCommodities() {
		return commodities;
	}

	public void setCommodities(List<Commodity> commodities) {
		this.commodities = commodities;
	}

	public String[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(String[] selectedList) {
		this.selectedList = selectedList;
	}


	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<Provider> getProviders() {
		return providers;
	}

	public void setProviders(List<Provider> providers) {
		this.providers = providers;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}
	@Resource(name="categoryService")
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public ProviderService getProviderService() {
		return providerService;
	}
	@Resource(name="providerService")
	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

	public String getJson1() {
		return json1;
	}

	public void setJson1(String json1) {
		this.json1 = json1;
	}

}
